package com.jianhua.backend.controller;

import com.jianhua.backend.domain.entity.Dishes;
import com.jianhua.backend.service.DishesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jianhua.backend.common.R;
import com.jianhua.backend.domain.dto.DishesDto;
import com.jianhua.backend.domain.entity.Dishes;
import com.jianhua.backend.domain.vo.PageVo;
import com.jianhua.backend.service.DishesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 存储所有菜品详细信息，包括营养成分等 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-07-02
 */
@RestController
@RequestMapping("/dishes")
public class DishesController {

    @Autowired
    private DishesService dishesService;

    // 获取所有菜品列表
    @GetMapping("/all")
    public List<Dishes> getAllDishes() {
        return dishesService.list();
    }

    // 根据ID获取单个菜品详情
    @GetMapping("/{id}")
    public Dishes getDishById(@PathVariable Integer id) {
        return dishesService.getById(id);
    }


    /**
     * 获取菜品列表接口
     * 根据传入的菜品查询条件，返回对应的菜品列表信息
     *
     * @param dishesDto 菜品查询条件，包含菜品名称、类型等
     * @return 返回包含菜品列表信息的响应对象R
     */
    @PostMapping("/list")
    public R listDishes(@RequestBody DishesDto dishesDto){
        PageVo pageVo = dishesService.listDishes(dishesDto);
        return R.success(pageVo);
    }

    /**
     * 更新菜品信息接口
     * 该接口用于接收前端传来的菜品数据，并更新数据库中对应的菜品信息。
     *
     * @param dishes 菜品对象，包含需要更新的菜品信息
     * @return 返回操作结果，成功则返回成功信息
     */
    @PostMapping("/update")
    public R updateDishes(@RequestBody Dishes dishes){
        dishesService.updateDishes(dishes);
        return R.success(null);
    }

    /**
     * 删除菜品接口
     * 根据提供的菜品ID删除对应的菜品
     *
     * @param id 菜品ID
     * @return 操作结果，成功返回R.success(null)
     */
    @DeleteMapping("/delete/{id}")
    public R deleteDishes(@PathVariable Integer id){
        dishesService.deleteDishes(id);
        return R.success(null);
    }

}
